//
//  HdeaderViewTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/17.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "HdeaderViewTableViewCell.h"
#import "IndexGridButton.h"
#import "ScrollViewItem.h"

@interface HdeaderViewTableViewCell()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIPageControl *pagecontrol;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation HdeaderViewTableViewCell
+(instancetype)header{
    HdeaderViewTableViewCell *cell=[[[NSBundle mainBundle]loadNibNamed:@"HdeaderViewTableViewCell" owner:nil options:nil] lastObject];
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
   
   self.timer= [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(pagechange) userInfo:nil repeats:YES];
    
}

-(void)pagechange{
    NSInteger page=self.pagecontrol.currentPage;
    page++;
    if (page==self.picarray.count) {
        page=0;
    }
    
    self.scrollview.contentOffset=CGPointMake(page*XHScreenwidth, 0);
}

-(void)setPicarray:(NSArray *)picarray{
    _picarray =picarray;
    if (_picarray==nil) {
        _picarray=@[@"guanggao1",@"guanggao2"];
    }
    
    [self setupScroll];
}
-(void)setupScroll{
    for (int i=0; i<self.picarray.count-1; i++) {
        UIImageView *imageview=[[UIImageView alloc]init];
        imageview.contentMode=UIViewContentModeScaleAspectFill;
        imageview.height=self.scrollview.height;
        imageview.y=0;
        imageview.x=i*XHScreenwidth;
        imageview.width=XHScreenwidth;
        imageview.clipsToBounds=YES;
        imageview.image=[UIImage imageNamed:self.picarray[i]];
        [self.scrollview addSubview:imageview];
        
    }
    ScrollViewItem *item=[ScrollViewItem scrollitem];
    item.width=XHScreenwidth;
    item.x=XHScreenwidth;
    item.height=self.scrollview.height;
    [self.scrollview addSubview:item];
    self.scrollview.contentSize=CGSizeMake(XHScreenwidth*self.picarray.count, 0);
    self.scrollview.delegate=self;
    self.scrollview.showsHorizontalScrollIndicator=NO;
    NSLog(@"%ld",self.scrollview.subviews.count);
    self.pagecontrol.numberOfPages=_picarray.count;
}

- (IBAction)girditemclick:(IndexGridButton *)sender {
    if([_delegate respondsToSelector:@selector(click:)]) {
        [_delegate click:sender.tag];
    }
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"%f",scrollView.contentOffset.x);
    int pageindex=scrollView.contentOffset.x/XHScreenwidth;
    self.pagecontrol.currentPage=pageindex;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}


@end
